/**
 * Copyright 2014  XCL-Charts
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @Project XCL-Charts
 * @Description Android图表基类库
 * @author XiongChuanLiang<br />(xcl_168@aliyun.com)
 * @Copyright Copyright (c) 2014 XCL-Charts (www.xclcharts.com)
 * @license http://www.apache.org/licenses/  Apache v2 License
 * @version 1.0
 */
package com.seocoo.worker.base.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import org.xclcharts.chart.DountChart;
import org.xclcharts.chart.PieData;
import org.xclcharts.common.DensityUtil;
import org.xclcharts.event.click.ArcPosition;
import org.xclcharts.renderer.XEnum;
import org.xclcharts.renderer.plot.PlotLegend;
import org.xclcharts.view.ChartView;

import java.util.LinkedList;

/**
 * @author XiongChuanLiang<br />(xcl_168@aliyun.com)
 * @ClassName DountChart01View
 * @Description 环形图例子
 */
public class DountChartView extends ChartView {

    private String TAG = "DountChart01View";
    private DountChart chart = new DountChart();

    LinkedList<PieData> lPieData = new LinkedList<PieData>();

    public DountChartView(Context context) {
        super(context);
        initView();
    }

    public DountChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public DountChartView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        chartRender();
    }

    protected int[] getPieDefaultSpadding() {
        int[] ltrb = new int[4];
        ltrb[0] = DensityUtil.dip2px(getContext(), 20); //left
        ltrb[1] = DensityUtil.dip2px(getContext(), 20); //top
        ltrb[2] = DensityUtil.dip2px(getContext(), 20); //right
        ltrb[3] = DensityUtil.dip2px(getContext(), 20); //bottom
        return ltrb;
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //图所占范围大小
        chart.setChartRange(w, h);
    }

    private void chartRender() {
        try {
            //设置绘图区默认缩进px值
            int[] ltrb = getPieDefaultSpadding();
            chart.setPadding(ltrb[0], ltrb[1] , ltrb[2], ltrb[3] + 60);

            //数据源
            chart.setDataSource(lPieData);
            chart.getCenterTextPaint().setColor(Color.rgb(242, 167, 69));

            //标签显示(隐藏，显示在中间，显示在扇区外面)
            chart.setLabelStyle(XEnum.SliceLabelStyle.BROKENLINE);
            chart.getLabelPaint().setColor(Color.BLACK);
            chart.getLabelPaint().setTextSize(35);

            //显示图例
            PlotLegend legend = chart.getPlotLegend();
            legend.show();
            legend.setType(XEnum.LegendType.ROW);
            legend.setHorizontalAlign(XEnum.HorizontalAlign.CENTER);
            legend.setVerticalAlign(XEnum.VerticalAlign.BOTTOM);
            legend.hideBox();
            legend.getPaint().setTextSize(25);

            //图背景色
            chart.setApplyBackgroundColor(true);
            chart.setBackgroundColor(Color.parseColor("#ffffff"));

            //内环背景色
            chart.getInnerPaint().setColor(Color.parseColor("#ffffff"));

            //显示边框线，并设置其颜色
            chart.getArcBorderPaint().setColor(Color.YELLOW);

            //可用这个修改环所占比例
//            chart.setInnerRadius(0.6f);
//            chart.setInitialAngle(90.f);

            //保存标签位置
            chart.saveLabelsPosition(XEnum.LabelSaveType.ALL);

            //激活点击监听
            chart.ActiveListenItemClick();
            chart.showClikedFocus();
            chart.setInnerRadius(0.5f);
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }

    public void chartDataSet( PieData pieData) {
        lPieData.add(pieData);
        //设置图表数据源
    }

    @Override
    public void render(Canvas canvas) {
        try {
            chart.render(canvas);
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_UP) {
            triggerClick(event.getX(), event.getY());
        }
        return true;
    }


    //触发监听
    private void triggerClick(float x, float y) {
        if (!chart.getListenItemClickStatus()) return;
        ArcPosition record = chart.getPositionRecord(x, y);
        if (null == record) return;

        PieData pData = lPieData.get(record.getDataID());

        boolean isInvaldate = true;
        for (int i = 0; i < lPieData.size(); i++) {
            PieData cData = lPieData.get(i);
            if (i == record.getDataID()) {
                if (cData.getSelected()) {
                    isInvaldate = false;
                    break;
                } else {
                    cData.setSelected(true);
                }
            } else
                cData.setSelected(false);
        }

        if (isInvaldate) this.invalidate();
    }


}
